package cn.edu.neosoft.sunbo509.finefood.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.menu.ActionMenuItemView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import java.util.List;
import cn.edu.neosoft.sunbo509.finefood.Adapter.MyDividerItemDecoration;
import cn.edu.neosoft.sunbo509.finefood.Adapter.ShopdetailAdapter;
import cn.edu.neosoft.sunbo509.finefood.BaseUrl;
import cn.edu.neosoft.sunbo509.finefood.Bean.CollectshopBean;
import cn.edu.neosoft.sunbo509.finefood.Bean.IscollectBean;
import cn.edu.neosoft.sunbo509.finefood.Bean.ShopdetailBean;
import cn.edu.neosoft.sunbo509.finefood.R;
import cn.edu.neosoft.sunbo509.finefood.Service.Service;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ShopdetailActivity extends AppCompatActivity {

    private RecyclerView sd_recycleview;
    private RecyclerView.LayoutManager sd_layoutmanager;
    private ShopdetailAdapter sd_adapter;
    private List<ShopdetailBean> list;
    private Retrofit retrofit;
    private int shop_id,bigflag;
    private  String user_id;
    private String shopname;
    private Service service;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopdetail);
        Intent intent=getIntent();
        shop_id=intent.getIntExtra("shopid",0);
        user_id=intent.getStringExtra("userid");
        shopname=intent.getStringExtra("shopname");
        initRecycleView();
        getShopdetail();
        isCollect();
    }

    private void initRecycleView() {
        sd_recycleview = (RecyclerView)findViewById(R.id.rv_shopdetail);
        //创建默认的线性LayoutManager
        sd_layoutmanager = new LinearLayoutManager(this);
        // 设置布局管理器
        sd_recycleview.setLayoutManager(sd_layoutmanager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        sd_recycleview.setHasFixedSize(true);
        sd_adapter = new ShopdetailAdapter(this);
        sd_recycleview.setAdapter(sd_adapter);
        sd_recycleview.addItemDecoration(new MyDividerItemDecoration(this,LinearLayoutManager.VERTICAL));
    }
    @Override  //加载菜单布局
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.collect, menu);
        return true;
    }
    @Override//选择菜单布局
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.collect:
                if(bigflag==0) {  //flag=0，表示未收藏
                    Collectshop();
                }
                if (bigflag==1) {  //flag=1,表示已经收藏
                    Uncollectshop();
                }
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void SetCollect(int flag){
        ActionMenuItemView item=(ActionMenuItemView)findViewById(R.id.collect);
       switch (flag){
           case 1:
               item.setBackgroundResource(R.drawable.xin1);
               bigflag=1;
               Toast.makeText(getApplicationContext(),"收藏成功",Toast.LENGTH_SHORT).show();
               break;
           case 2:
               item.setBackgroundResource(R.drawable.xin0);
               Toast.makeText(getApplicationContext(),"收藏失败",Toast.LENGTH_SHORT).show();
               break;
           case 3:
               item.setBackgroundResource(R.drawable.xin0);
               bigflag=0;
               Toast.makeText(getApplicationContext(),"取消收藏成功",Toast.LENGTH_SHORT).show();
               break;
           case 4:
               item.setBackgroundResource(R.drawable.xin1);
               Toast.makeText(getApplicationContext(),"取消收藏失败",Toast.LENGTH_SHORT).show();
               break;
           default:
               Toast.makeText(getApplicationContext(),"异常",Toast.LENGTH_SHORT).show();
               break;
       }
    }
    //获取店铺详情
    public void getShopdetail(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BaseUrl.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service =retrofit.create(Service.class);
        Call<List<ShopdetailBean>>call=service.getDatail(shop_id);
        call.enqueue(new Callback<List<ShopdetailBean>>() {
            @Override
            public void onResponse(Call<List<ShopdetailBean>> call, Response<List<ShopdetailBean>> response) {
                list=response.body();
                sd_adapter.setList(list);
                sd_adapter.setUserid(user_id);
                sd_adapter.setShopname(shopname);
            }
            @Override
            public void onFailure(Call<List<ShopdetailBean>> call, Throwable t) {
                Toast.makeText(getApplicationContext(),"异常",Toast.LENGTH_SHORT).show();
            }
        });
    }
    //判断是否收藏过店铺 ，接口第三个参数是0
    public void isCollect(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BaseUrl.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service=retrofit.create(Service.class);
        Call<IscollectBean>call=service.getValue(Integer.valueOf(user_id),shop_id,0);
        call.enqueue(new Callback<IscollectBean>() {
            @Override
            public void onResponse(Call<IscollectBean> call, Response<IscollectBean> response) {
                switch (response.body().getCollected()) {
                    case "1":
                        bigflag=1;//已经收藏
                        break;
                    case "0":
                        bigflag=0;//没有收藏
                        break;
                    default:
                        Toast.makeText(getApplicationContext(), "异常", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
            @Override
            public void onFailure(Call<IscollectBean> call, Throwable t) {
                 Toast.makeText(getApplicationContext(),"异常",Toast.LENGTH_SHORT).show();
            }
        });
    }
    //收藏店铺
    public void Collectshop(){
    retrofit=new Retrofit.Builder()
            .baseUrl(BaseUrl.BASEURL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
       service=retrofit.create(Service.class);
        Call<CollectshopBean>call=service.getshopValue(Integer.valueOf(user_id),shop_id);
        call.enqueue(new Callback<CollectshopBean>() {
            @Override
            public void onResponse(Call<CollectshopBean> call, Response<CollectshopBean> response) {
                if(response.body().getSuccess().equals("1"))
                    SetCollect(1);
                else
                    SetCollect(2);
        }
            @Override
            public void onFailure(Call<CollectshopBean> call, Throwable t) {
                Toast.makeText(getApplicationContext(),"异常",Toast.LENGTH_SHORT).show();
            }
        });
    }
    //取消收藏店铺
    public void Uncollectshop(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BaseUrl.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service=retrofit.create(Service.class);
        Call<CollectshopBean>call=service.getshopValue(Integer.valueOf(user_id),shop_id);
        call.enqueue(new Callback<CollectshopBean>() {
            @Override
            public void onResponse(Call<CollectshopBean> call, Response<CollectshopBean> response) {
                if(response.body().getSuccess().equals("1"))
                    SetCollect(3);
                else
                    SetCollect(4);
            }
            @Override
            public void onFailure(Call<CollectshopBean> call, Throwable t) {
                Toast.makeText(getApplicationContext(),"异常",Toast.LENGTH_SHORT).show();
            }
        });
    }

}
